import 'package:flutter/material.dart';
import 'package:flutter_basic_learning/utils/CommonWidget.dart';
import 'package:get/get.dart';

class BottomSheetPage extends StatefulWidget {
  const BottomSheetPage({super.key});

  @override
  State<BottomSheetPage> createState() => _BottomSheetPageState();
}

class _BottomSheetPageState extends State<BottomSheetPage> {
  Widget listItem(
      {String title = '白天模式',
      IconData iconData = Icons.light_mode,
      ThemeData? themeDataFunc}) {
    Widget body = ListTile(
      leading: Icon(
        iconData,
        color: Get.isDarkMode ? Colors.white54 : Colors.black54,
      ),
      title: Text(
        title,
        style: TextStyle(
          color: Get.isDarkMode ? Colors.white54 : Colors.black54,
        ),
      ),
      onTap: () {
        Get.changeTheme(ThemeData.light()); //更改主题
        Get.back(); //返回上一级页面
      },
    );
    body = Container(
        width: double.infinity,
        color: Get.isDarkMode ? Colors.white54 : Colors.black54,
        child: body);
    return body;
  }

  Widget bottomSheetBuilder() {
    Widget body = Column(
      mainAxisSize: MainAxisSize.min,
      mainAxisAlignment: MainAxisAlignment.center,
      spacing: 5,
      children: [
        listItem(
            title: '深色模式',
            iconData: Icons.dark_mode,
            themeDataFunc: ThemeData.dark()),
        listItem(
            title: '浅色模式',
            iconData: Icons.light_mode,
            themeDataFunc: ThemeData.light()),
      ],
    );
    body = SizedBox(width: double.infinity, height: 200, child: body);
    return body;
  }

  Widget body() {
    Widget body = ElevatedButton(
      onPressed: () {
        Get.bottomSheet(
          bottomSheetBuilder(), //模态框弹窗的内容
          backgroundColor: Colors.transparent, //模态框背景色
          elevation: 1, //模态框阴影
          persistent: false, //是否添加到路由中
          clipBehavior: Clip.hardEdge, //裁剪方式
          barrierColor: Colors.transparent, //弹出层背景色
          ignoreSafeArea: false, //是否忽略安全适配
          isScrollControlled: false, //是否支持全屏弹窗，默认false
          useRootNavigator: false, //是否使用跟导航，默认false
          isDismissible: true, //点击空白关闭弹窗，默认true
          enableDrag: true, //是否可以拖动关闭，默认true
          // settings: const RouteSettings(//路由设置
          //   name: 'BottomSheetPage',
          //   arguments: 'BottomSheetPage',
          // ),
          enterBottomSheetDuration:
              const Duration(milliseconds: 500), //弹窗模态框的动画时间
          exitBottomSheetDuration:
              const Duration(milliseconds: 500), //模态框退出的动画时间
        );
      },
      child: commonTitle('弹窗底部模态框', isBold: false, fontSize: 16),
    );
    body = Center(child: body);
    return body;
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: commonAppBar('BottomSheet 底部模态框 + 更改主题'),
      body: body(),
    );
  }
}
